<template>
  <basic-container>
    <el-form :inline="true" :model="query" class="demo-form-inline">
      <el-row>
        <el-form-item label="项目编号:">
          <el-input v-model="query.projectCode" clearable placeholder="项目编号" size="small"></el-input>
        </el-form-item>
        <el-form-item label="项目名称:">
          <el-input v-model="query.projectName" clearable placeholder="项目名称"></el-input>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item>
          <el-button size="small" type="primary" icon="el-icon-search" @click="onSearch" :loading="loading">搜索
          </el-button>
          <el-button size="small" icon="el-icon-delete" @click="searchReset" :loading="loading">清空</el-button>
        </el-form-item>
      </el-row>
    </el-form>
    <div class="btn-operate">
      <el-button @click="addReportForm" type="primary" size="small" icon="el-icon-plus">
        撰写审核初稿
      </el-button>
    </div>
    <el-table v-loading="loading" :data="tableData" border
              highlight-current-row @cell-click="handleCellClick" style="width: 100%">
      <el-table-column label="项目编号" prop="projectCode" width="140">
      </el-table-column>
      <el-table-column label="项目名称" prop="projectName" width="270">
        <template v-slot="scope">
            <span :style="{ color: scope.row.projectProgress === 7 ? '#f16262' : '' }">
              {{ scope.row.projectName }}
              <template v-if="scope.row.projectProgress === 7">
                <div style="display: inline-block">【已终止】</div>
              </template>
            </span>
        </template>
      </el-table-column>
      <el-table-column prop="reportStatus" label="报告类型">
        初稿
      </el-table-column>
<!--      <el-table-column label="报告文号">-->
<!--        <template slot-scope="scope">-->
<!--          〔{{scope.row.reportYear}}〕{{scope.row.reportCode}}号-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="审批状态" prop="auditStatus">
        <template slot-scope="scope">
          <el-tag :type="scope.row.auditStatus | auditStatusTag">{{ scope.row.auditStatus | auditStatus }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="170">
        <template slot-scope="scope">
          <el-button v-if="permissions.engineering_draft_get" type="text" size="small" icon="el-icon-view" @click="showViewBox(scope.row)">查看</el-button>
          <el-button v-if="permissions.engineering_draft_add && (scope.row.auditStatus === 0 || scope.row.auditStatus === 2)" type="text" size="small" icon="el-icon-edit" @click="showEditBox(scope.row)">编辑</el-button>
<!--          <el-button v-if="scope.row.taskId" type="text" size="small" icon="el-icon-edit" @click="showTaskAuditTaskDialog(scope.row)">审批</el-button>-->
<!--          <el-button v-if="scope.row.auditStatus > 0" type="text" size="small" icon="el-icon-view" @click="showTaskAuditLogDialog(scope.row)">审批日志</el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="sizeChange"
        @current-change="currentChange"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 30, 40 ,50 ,100]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>

    <el-dialog top="5vh" :title="formTitle" :visible.sync="viewBox"
               :append-to-body="true" :before-close="closeViewBox" width="1200px">
      <AuditEngineeringReportDraftView :params="form"></AuditEngineeringReportDraftView>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeViewBox" size="small" icon="el-icon-close">关闭</el-button>
      </span>
    </el-dialog>

    <TaskAuditDialog :dialog="taskAuditDialog" @setValue="setTaskAuditTaskDialog" @closeDialog="closeTaskAuditTaskDialog" ref="taskAuditDialog" />
    <TaskAuditLogDialog :dialog="taskAuditLogDialog" :process-id="processId" @closeDialog="closeTaskAuditTaskLogDialog"/>
    <table-form ref="reportForm" @cancelDialog="closeReportFormDialog"></table-form>
  </basic-container>
</template>
<script>
import TableForm from "@/components/form/engineeringDraftForm.vue";
import TaskAuditDialog from "@/components/act/TaskAuditDialog.vue";
import TaskAuditLogDialog from "@/components/act/TaskAuditLogDialog";
import {fetchList, getReport} from "@/api/adis/report/reportOpinion";
import {mapGetters} from "vuex";
import AuditEngineeringReportDraftView from "@/components/actForm/AuditEngineeringReportDraftView";

export default {
  components: {AuditEngineeringReportDraftView, TaskAuditDialog, TaskAuditLogDialog, TableForm},
  computed:{
    ...mapGetters(["permissions"])
  },
  data(){
    return{
      query: {},
      form: {},
      tableData:[],
      loading:false,
      taskAuditDialog: false,
      taskAuditLogDialog: false,
      processId : '',
      formTitle: '',
      page: {
        size: 10,
        current: 1,
        total: 0
      },
      viewBox: false,
    }
  },
  created() {
    this.onLoad(this.page);
    this.Authorization = 'Bearer ' + this.$store.getters.access_token
  },
  methods:{
    handleCellClick(row, column) {
      if (row && column.label !== "操作"){
        this.showViewBox(row)
      }
    },

    addReportForm(row){
      this.$nextTick(() => {
        this.$refs.reportForm.init(row,true);
      })
    },
    closeReportFormDialog(){
      this.onLoad(this.page);
    },
    onSearch(){
      this.page.current = 1;
      this.onLoad(this.page);
    },
    searchReset() {
      this.query = {};
      this.page.current = 1;
      this.onLoad(this.page)
    },
    showEditBox(row) {
      this.addReportForm(row);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      this.query.reportStatus = '初稿';
      this.query.isEngineering = 1;
      fetchList(Object.assign(params, this.query, page)).then(res => {
        this.loading = false;
        const data = res.data.data;
        this.page.total = data.total;
        this.tableData = data.records;
      }, () => {
        this.loading = false;
      });
    },
    sizeChange(size) {
      this.page.size = size;
      this.onLoad(this.page);
    },
    currentChange(current) {
      this.page.current = current;
      this.onLoad(this.page);
    },

    /**=========审批相关==begin===========*/
    showTaskAuditTaskDialog(row) {
      this.taskAuditDialog = true;
      this.$refs['taskAuditDialog'].initData({taskId: row.taskId, processId: row.processId});
    },
    closeTaskAuditTaskDialog() {
      this.taskAuditDialog = false;
    },
    setTaskAuditTaskDialog() {
      this.taskAuditDialog = false;
      this.onLoad(this.page);
    },
    showTaskAuditLogDialog(row) {
      this.processId = row.processId;
      this.taskAuditLogDialog = true;
    },
    closeTaskAuditTaskLogDialog() {
      this.taskAuditLogDialog = false;
    },
    /**=========审批相关==end===========*/

    showViewBox(row) {
      getReport(row.reportId).then((res)=>{
        this.form = res.data.data;
        this.formTitle = row.projectName;
        this.viewBox = true;
      })
    },
    closeViewBox() {
      this.viewBox = false;
    },
    handlePreview(file) {
      const a = document.createElement('a');
      let url = file.url ? file.url : file.response.data.url;
      console.log(url);      // let fileName = '你想要的文件名.docx'; // 这里设置你想要的文件名，包括扩展名
      a.setAttribute('download', file.name);
      a.setAttribute('href', url);
      a.click();
    },
  }
}
</script>
